package com.game.thrones.service.web.impl;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.game.thrones.constant.Constants;
import com.game.thrones.domain.KbmContent;
//import com.game.thrones.domain.department.DepartmentDO;
import com.game.thrones.domain.webpage.WebEndemicDO;
import com.game.thrones.service.SysBaseServiceImpl;
//import com.game.thrones.service.department.DepartmentService;
import com.game.thrones.service.web.WebEndemicService;
import com.game.thrones.vo.EndemicVO;

@Service("webEndemicService")
public class WebEndemicServiceImpl extends SysBaseServiceImpl<WebEndemicDO, Integer> implements WebEndemicService {

	private Logger log = LoggerFactory.getLogger(getClass());
//	@Autowired
//	DepartmentService departmentService;
	@Override
	public EndemicVO findEndemicVOByRequest(HttpServletRequest request) {

		EndemicVO endemicVO = new EndemicVO();
		endemicVO.setEndemicId(137);
		endemicVO.setEndemicName("北京");
		return endemicVO;
/*
		KbmContent result = (KbmContent) request.getSession().getAttribute(Constants.CURRENT_SYS_USER_INFO);
		if (result!= null&&(!"testToken".equals(result.getToken()))) {
			EndemicVO vo = new EndemicVO();
			vo.setEndemicId(result.getEndemicId());
			vo.setEndemicName(result.getEndemicName());
			vo.setCurrentUserId(result.getCurrentUserId());
			return vo;
		}


		String webEndemicId = request.getHeader("WEB-ENDEMIC-ID");
		log.info("当前地方中心id为："+webEndemicId);
		if(StringUtils.isNotEmpty(webEndemicId)){
			return findEndemicVOBy(Integer.parseInt(webEndemicId));
		}
		String currentIp=StringUtils.getIpAddress(request);
		EndemicVO endemicVO = (EndemicVO) JedisUtil.getObj(Constants.IP_Endemic + currentIp);
		if(endemicVO!=null){
			return endemicVO;
		}
		endemicVO = new EndemicVO();
		endemicVO.setEndemicId(2);
		endemicVO.setEndemicName("总部");

			String region="北京";
			if(currentIp.indexOf("192.")==0){

			}else {
				try {
				HttpUtil hu = new HttpUtil();
				String json = hu.getResponse("http://ip.taobao.com/service/getIpInfo.php?ip=" + currentIp, "");
				Map<String, Object> map = JsonUtil.jsonToMap(json);
				Map<String, Object> data = (Map<String, Object>) map.get("data");
				region = (String) data.get("region");
				if (region.indexOf("市") > -1) {
					region = region.substring(0, region.length() - 1);
				}
				} catch (Exception e) {
					log.error("获取当前ip地方中心出错"+e.getMessage());
					region="北京";
				}
			}
			List<DepartmentDO> entityList = departmentService.findEndemic();
			for(DepartmentDO entity:entityList){
				String endemicName = entity.getName();
				if(endemicName.indexOf(region)>-1){
					endemicVO.setEndemicId(entity.getId());
					endemicVO.setEndemicName(entity.getName());
				}
			}

		JedisUtil.setObj(Constants.IP_Endemic + currentIp,endemicVO,7200);
		return endemicVO;
*/
	}
	public EndemicVO findEndemicVOBy(Integer id){
//		EndemicVO endemicVO = (EndemicVO) JedisUtil.getObj(Constants.ID_Endemic  + id);
		EndemicVO endemicVO = null;
		if(endemicVO!=null){
			return endemicVO;
		}
		endemicVO = new EndemicVO();
		endemicVO.setEndemicId(2);
		endemicVO.setEndemicName("总部");
//		DepartmentDO department = departmentService.findOneById(id);
//		if(department!=null){
//			endemicVO.setEndemicId(department.getId());
//			endemicVO.setEndemicName(department.getName());
//		}
//		JedisUtil.setObj(Constants.ID_Endemic +id,endemicVO );
		return endemicVO;
	}
	public KbmContent findKbmContent(HttpServletRequest request){
		EndemicVO endemicVO=findEndemicVOByRequest(  request);
		KbmContent kbmContent = new KbmContent();
		kbmContent.setEndemicId(endemicVO.getEndemicId());
		kbmContent.setCurrentUserId(-2);
		//KbmContent kbmContent = new KbmContent();
		//kbmContent.setEndemicId(2);
		//kbmContent.setCurrentUserId(-2);
		return kbmContent;
	}
}
